<template>
  <a-modal
    :visible="visible"
    :title="t('add')"
    :cancelText="t('cancel')"
    :okText="t('submit')"
    @ok="submit"
    @cancel="cancel()"
  >
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >     
      <a-form-item ref="name" :label="t('name')" name="name">
        <a-input v-model:value="formState.name" />
      </a-form-item> 
      <a-form-item  ref="desc" :label="t('desc')" name="desc">
        <a-input v-model:value="formState.desc" />
      </a-form-item>  
      
    </a-form>
  </a-modal>
</template>
<script>
import { message } from "ant-design-vue";
import { useI18n  } from "vue-i18n";
import { add } from "@/core/api/module/role";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";
import { useStore } from 'vuex';
export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
  },
  emit: ["close"],
  setup(props, context) {
    const { t } = useI18n();
    const state = reactive({
      depts: [], 
      showPath: true, 
    });

    const formRef = ref();

	  const store = useStore();
    const formState = reactive({
      token: store.getters.token,
      username:"",
      password:"",
      nickname:"",
      status:0,
    });
  
    const formRules = {
      name: [{ required: true , message: t('Please input user name'), trigger: "blur" }],
    };

    const submit = (e) => {     

      //console.log(formState.token);
     
      formRef.value
        .validate()
        .then(() => { 
          add(toRaw(formState)).then((response) => { 

            if (response.code==0) {
              message.success({ content: t('Saved successfully!'), duration: 1 }).then(() => {
                cancel(true);
              });
            } else {
              message.error({ content: response.msg, duration: 1 }).then(() => {
                cancel(true);
              });
            }
          });
        })
        .catch((error) => {
          console.log("error", error);
        });
    };

    const cancel = (e=false) => {
      formRef.value.resetFields();
      context.emit("close", e);
    };
 
   /*
    const loadPower = () => {
      tree({}).then((response) => {
        response.data = [{
          id:0,
          title: "顶级菜单",
          children: response.data
        }]
        state.powers = response.data;
      });
    };
    */


    //loadPower();

    return {
      t,
      state,
      submit,
      cancel,
      formRef,
      formState,
      formRules, 
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },    
    };
  },
});
</script>